package com.tjhk.java.tech.thread.completable;

import java.util.concurrent.*;

public class CompletionServiceTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService exc = Executors.newFixedThreadPool(2);
        CompletionService<Integer> cs = new ExecutorCompletionService<>(exc);
        cs.submit(()->getPrice1());
        cs.submit(() -> getPrice2());
        for (int i = 0; i < 2; i++) {
            Integer r = cs.take().get();
            exc.execute(()->save(r));
        }
        System.out.println("done");
    }
    public static Integer getPrice1(){
        try {
            System.out.println("正在获取价格-1");
            Thread.sleep(3000);
            System.out.println("!!!!!!!!!!!");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return 10;
    }
    public static Integer getPrice2(){
        try {
            System.out.println("正在获取价格-2");
            Thread.sleep(30000);
            System.out.println("##########");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return 7878;
    }

    public static void save(Integer a){
        System.out.println("save: "+a);
    }
}
